// ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 

OAA_CI.color = OAA_CI.color || {}; 

/** ================================================================
*
* ColorContrasts Module initialization
*
* =============================================================== */

OAA_CI.color.last_index = 0;

/** ================================================================
*
* onLoad
*
* @desc Initializes color_contrast module
*
* @return nothing
*
* =============================================================== */

OAA_CI.color.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.color_nls_strings = document.getElementById("ID_STRINGBUNDLE_COLOR_CONTRAST");        

//  OpenAjax.a11y.console("Loading color contrast strings: " + OAA_CI.color_nls_strings);
   
  OAA_CI.color.onShowColorContrastsCommand();
}; 

/** ================================================================
*
* onShowColorContrastsCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with color contrast information
*
* @return nothing
*
* =============================================================== */

OAA_CI.color.onShowColorContrastsCommand = function(e) {

  var i, j;

  OAA_CI.evaluateDocument();   
  
  var color_contrast_cache = OAA_CI.evaluation.dom_cache.color_contrast_cache;  
  
  color_contrast_cache.sortCCRItems(true);  
  
  var color_contrast_items = color_contrast_cache.color_contrast_items;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_COLOR_CONTRAST');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var color_contrast_items_len = color_contrast_items.length;
  
  OpenAjax.a11y.console("ColorContrast children object: " + color_contrast_items_len);
  
  var xul_treeitem = null;
  
  var cci = null;
  var des = null;
  var des_len = 0;
  var de = null;
  var value = false;
  
  for (i=0; i < color_contrast_items_len; i++ ) { 

    cci     = color_contrast_items[i];
    des     = cci.dom_elements;
    des_len = des.length;

    value = "cci-" + i;

    xul_treeitem = OAA_CI.createXULElement('treeitem', ["container=true","open=false"]);

    OAA_CI.addTreeRow(xul_treeitem, [des_len, cci.color, cci.background_color, cci.color_contrast_ratio, cci.background_image ], cci.cache_id );

    var xul_sub_treechildren = OAA_CI.createXULElement('treechildren',[]);

    for(j=0; j<des_len; j++) {
      de = des[j];
      OAA_CI.addTreeItem(xul_sub_treechildren, [de.tag_name, de.computed_style.color_hex, de.computed_style.background_color_hex, de.computed_style.color_contrast_ratio, de.computed_style.background_image ], de.cache_id );
    } // loop
    
    xul_treeitem.appendChild(xul_sub_treechildren);
    
    xul_treechildren.appendChild(xul_treeitem);

  } // end loop  
    
  OAA_CI.updateXULTime(start_time);
    
};  


/** ================================================================
*
* onColorContrastElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the ColorContrastCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.color.onColorContrastItemSelectChanged = function (event) {

  var de;
  var cci;
  var cs;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var element_cache = OAA_CI.evaluation.dom_cache.element_cache;  
  var color_contrast_cache = OAA_CI.evaluation.dom_cache.color_contrast_cache;  
  var xul_tree = document.getElementById('ID_TREE_COLOR_CONTRAST');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    cci = color_contrast_cache.getColorContrastItemById(cache_id);

    if (cci) {
      de = cci.dom_elements[0];
    }
    else {
      de = element_cache.getDOMElementByCacheId(cache_id);
    }  
    
    if (de) { 
      cs = de.computed_style;

      OAA_CI.color.showColorContrastItemProperties(cs);
         
      OAA_CI.highlightCacheItem(de);         

    }  
  } 

};


/** ================================================================
*
* showColorContrastItemProperties
*
* @desc Updates color contrast property information in tabpanel
*
* @return nothing
*
* =============================================================== */

OAA_CI.color.showColorContrastItemProperties = function ( item ) {

  var xul_treechildren = document.getElementById('ID_TREECHILDREN_COLOR_CONTRAST_PROPERTIES');
  
  OAA_CI.removeChildNodes(xul_treechildren);
  
  var sample_text = document.getElementById('ID_SAMPLE_TEXT');

  if (item) {
  
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('color'),               item.color);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_color'),    item.background_color);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('contrast'),            item.color_contrast_ratio);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('font_family'),         item.font_family);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('font_weight'),         item.font_weight);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('font_size'),           item.font_size);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_image'),    item.background_image);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_repeat'),   item.background_repeat);
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_position'), item.background_position);
 
    var style = "";
    style += "font-family: "           + item.font_family;
    style += "; font-size: "           + item.font_size;
    style += "; font-weight: "         + item.font_weight;
    style += "; color: "               + item.color;
    style += "; background-color: "    + item.background_color;
    style += "; background-image: "    + item.background_image;
    style += "; background-position: " + item.background_position;
    style += "; background-repeat: "   + item.background_repeat;
  
    sample_text.setAttribute("style", style); 
  } 
  else {

    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('color'),               OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_color'),    OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('contrast'),            OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('font_family'),         OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('font_weight'),         OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('font_size'),           OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_image'),    OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_repeat'),   OAA_CI.color_nls_strings.getString('unknown'));
    OAA_CI.addTreeItemPropertyNameAndValue(xul_treechildren, OAA_CI.color_nls_strings.getString('background_position'), OAA_CI.color_nls_strings.getString('unknown'));

  }

    sample_text.value = "Sample Text";  

};